<?php
class caf_upload extends controller{

	function caf_upload()
	{
	 	parent::Controller();
		$this->load->helper(array('url','form','file'));
		$this->load->model('caf_model');
		
	}
	
	function upload_outward()
	{
		$displayform ='';
		$displayform .='<div id=dashboard ><span>Data Entry</span> <a href='.base_url().'/idea_home/dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" id="back"" alt="back" ONCLICK="history.go(-1)" /></div>';
		$displayform .= '<div id=upload>';
		$attr = array('class'=>'add_outward','id'=>'add_upload');
		$displayform .= form_open_multipart('caf_upload/save_outward',$attr);
	    	
 	    	
	    	$displayform .= '<fieldset id="upload_caf" style="width:40%;">';
	    	$displayform .= ' <legend style="color:black;">Upload File</legend>';
	    	$displayform .= '<center><table cellspacing="20px" >';
	    	$displayform .= '<tr><td align=left><img src="'.base_url().'images/upload.png" alt=upload /></td>';
		$displayform .= '<td align=left><input type="file" name="userfile" size="20" class="required" accept="txt" />';
		$displayform .= '</td></tr>';
		$displayform .='<tr><td colspan=2 align=center><input class="submit" type="submit" value="Save" />';
		$displayform .='<input type="reset" value="Cancel" /></td>';
		$displayform .= '</tr></table>';
		
		$displayform .= '</fieldset>';
		$displayform .= '</form>';
		
		
		$displayform .= '</div>';
		$data = array('formdisplay'=>$displayform);
		$this->template->add_js('js/jquery-latest.js');	
		$this->template->add_js('js/jquery.validate.js');
		$this->template->add_js('js/jquery.ui.progressbar.js');
		$this->template->add_js('$(document).ready(function(){
	 		   $("#add_upload").validate();
	  			});
		','embed');
		

			$this->template->write_view('header','header', $data, True);
			$this->template->write_view('content', 'other_general');
			$this->template->render();
	}
	
	function save_outward()
		{	
		$newfolder =  date('ymd').'_'.time();
		
		 	
			$dir = './uploads/'.$newfolder.'/';

			 if (!is_dir($dir)) { 
	               		 $theupload_path = mkdir( $dir , 0777);
			}

		$config['upload_path'] = $dir;
		$config['allowed_types'] = 'txt';
		$config['max_size']	= '2097152';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
		$this->load->library('upload',$config);
				
		if (!$this->upload->do_upload()) 
				{
					$error = array('error' => $this->upload->display_errors());
					print_r($error);
				}    
				else
				{	
					$filename = array('upload_data' => $this->upload->data());
				
					$this->load->model('caf_model');
					$filepath = $dir.$filename['upload_data']['file_name'];
					$this->caf_model->save_caf($filepath);
					redirect('caf_upload/upload_outward');					
				}
				
		}
		
		
		//to upload excel sheet...
/*	function upload_outward()
	{
		$displayform ='';
		$displayform .='<div id=dashboard ><span>Data Entry</span> <a href='.base_url().'/idea_home/dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" id="back"" alt="back" ONCLICK="history.go(-1)" /></div>';
		$displayform .= '<div id=upload>';
		$attr = array('class'=>'add_outward','id'=>'add_upload');
		$displayform .= form_open_multipart('caf_upload/save_outward',$attr);
	    	
 	    	
	    	$displayform .= '<fieldset id="upload_caf" style="width:40%;">';
	    	$displayform .= ' <legend style="color:black;">Upload File</legend>';
	    	$displayform .= '<center><table cellspacing="20px" ><tr>';
	    	$displayform .= '<tr><td align=left><img src="'.base_url().'images/upload.png" alt=upload /></td>';
		$displayform .= '<td align=left><input type="file" name="userfile" size="20" class="required" accept="xls" />';
		$displayform .= '</td></tr>';
		$displayform .='<tr><td colspan=2 align=center><input class="submit" type="submit" value="Save" />';
		$displayform .='<input type="reset" value="Cancel" /></td>';
		$displayform .= '</tr></table>';
		
		$displayform .= '</fieldset>';
		$displayform .= '</form>';
		
		
		$displayform .= '</div>';
		$data = array('formdisplay'=>$displayform);
		$this->template->add_js('js/jquery-latest.js');	
		$this->template->add_js('js/jquery.validate.js');
		$this->template->add_js('js/jquery.ui.progressbar.js');
		$this->template->add_js('$(document).ready(function(){
	 		   $("#add_upload").validate();
	  			});
		','embed');
		

			$this->template->write_view('header','header', $data, True);
			$this->template->write_view('content', 'other_general');
			$this->template->render();
	}
	
	function save_outward()
		{
		
		 	
			$dir = './uploads/';

			 if (!is_dir($dir)) { 
	               		 $theupload_path = mkdir( $dir , 0777);
			}

		$config['upload_path'] = $dir;
		$config['allowed_types'] = 'xls';
		$config['max_size']	= '2097152';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
		$this->load->library('upload',$config);
				
		if (!$this->upload->do_upload()) 
				{
					$error = array('error' => $this->upload->display_errors());
					print_r($error);
				}    
				else
				{	
					$filename = array('upload_data' => $this->upload->data());
				
					require_once 'excel_reader2.php';

					$this->load->model('caf_model');
					$this->caf_model->save_caf($filename['upload_data']['file_name']);
										
				}
				
		}
					
	
	*/
}
